import crypto from 'crypto';

export function  rsaEncrypt(message) {  
  const publicKey = '-----BEGIN PUBLIC KEY-----****-----END PUBLIC KEY-----'   //your public key
  const buffer = Buffer.from(message, 'utf8');
  const encrypted = crypto.publicEncrypt({key:publicKey, padding : crypto.constants.RSA_PKCS1_PADDING}, buffer)
  return  encrypted.toString("base64");
  
  function rsaEncryptToLong(message) {
    const padding = 117;
    const count = message.length;
    const num = Math.floor(count / padding);
    const remainder = count % padding;
    let str = '';
    for (let i = 0; i < num; i++) {
      const value = message.slice(i * padding, i * padding + padding);
      str += rsaEncrypt(value);
      str += ';';
    }
    if (remainder !== 0) {
      const value = message.slice(-remainder);
      str += rsaEncrypt(value);
      str += ';';
    }
    if (str.length > 0) str = str.slice(0, str.length - 1);
    return str;
  }
}